<template src="./template.html"></template>
<style src="./style.css" scoped></style>
<script>
    import addressBorderBg from "assets/images/address-info-line.png";
    import api from "api/api";
    import modal from "utils/modal";
    import jsbridge from "utils/jsbridge";
    import navigator from "utils/navigator";
    import notification from "utils/notification";
    import constant from "constants/constant";

    const globalEvent = weex.requireModule('globalEvent');

    export default{
        data(){
            return{
                addressBorderBg: addressBorderBg,
                oid: null, //订单编号获取订单详情
                detailData: {},
                userAddressInfo: {},
                recordList:null
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            if(config.oid) self.oid = config.oid;

            self.sendHttp(self.oid,res=>{
                self.detailData = res.data;
                if(self.detailData && self.detailData.address) {
                    self.userAddressInfo = {
                        userName: self.detailData.address.name, //用户名
                        phone: self.detailData.address.phone, //联系电话
                        plot: self.detailData.address.neighbourhood, //小区名称
                        addressInfo: self.detailData.address.addrinfo,//地址详情
                        areaPoint: self.detailData.address.issueinfo //自提点信息
                    }
                }
                if(self.detailData && self.detailData.records){
                    self.recordList = self.getRecordList(self.detailData.records);
                }
            });

            //监听待付款订单支付成功事件
            globalEvent.addEventListener(constant.EVENT_ORDER_PAY_SUCCESS, function (e) {
                let _oid = e.data || null;
                if(self.oid == _oid) self.reloadDetail();
            });

            //监听订单评价事件
            globalEvent.addEventListener(constant.EVENT_ORDER_RATE, function (e) {
                self.reloadDetail();
            });


            //监听商品退款事件
            globalEvent.addEventListener(constant.EVENT_ORDER_REFUND, function (e) {
                self.reloadDetail();
            });
        },
        methods: {
            sendHttp(oid,callback){
                api.sendHttp('orderDetailApi',{oid:oid}).then(res=>{
                    if(typeof callback == "function") callback(res);
                },
                res=>{
                    modal.toast(res.msg || "获取订单详情失败");
                });
            },
            getRecordList(record){
                if(record){
                    record = record.replace("<p>","").replace("</p>","");
                    return record.split("<br />");
                }
            },
            delOrder(order){
                let self = this;
                order.status = "已删除";
                notification.send(constant.EVENT_ORDER_DETAIL,order);
                navigator.pop();
            },
            updateOrder(order){
                let self = this;
                self.detailData = order;
                notification.send(constant.EVENT_ORDER_DETAIL,order);
            },
            reloadDetail(){
                let self = this;
                let oid = self.oid;
                self.sendHttp(oid,res=>{
                    self.detailData = res.data;
                    if(self.detailData && self.detailData.records){
                        self.recordList = self.getRecordList(self.detailData.records);
                    }

                    notification.send(constant.EVENT_ORDER_DETAIL,order);
                });
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_ORDER_PAY_SUCCESS);
            globalEvent.removeEventListener(constant.EVENT_ORDER_RATE);
            globalEvent.removeEventListener(constant.EVENT_ORDER_REFUND);
        }
    }
</script>
